#!/bin/bash
#########################################################################
# File Name: multis_moniord to monitor multis_admin running status, 
#       if they no running start them
# Author:
# mail:
# Created Time: Sun 24 July 2016 14:01:55 PM CST
#########################################################################

#chkconfig: 345 98 02
#description: multis_moniord Demon Startup Script

PROG=multis_admin
MONITOR_PROG_PATH=/usr/local/bin
MONITOR_PROG=multis_monitor
LOG_PATH=/etc/multicast

RETVAL=0

status() 
{
    pgrep -x $MONITOR_PROG > /dev/null
    if [ $? -ne 0 ] ; then
        echo "$MONITOR_PROG is not running"
    else
        echo "$MONITOR_PROG is running"
    fi
}

start() 
{
    if [ `id -u` -ne 0 ]; then
        echo "****** Critical ERROR: Please re-run as root or sudo ******"
        exit 1
    fi

    #if multis_admin already exists, exit
    pgrep -x $MONITOR_PROG > /dev/null
    if [ $? -eq 0 ] ; then
        echo "vnet_xgw_monitor is already running, please stop it first"
        return $RETVAL
    fi
   
    #start vnet_xgw_monitor daemon
    echo  "Starting ${MONITOR_PROG} daemon..."
    logger -p user.warning -t "${MONITOR_PROG}" "${MONITOR_PROG} is starting" $(date +"%T %D")
    #nohup ${MONITOR_PROG_PATH}/$MONITOR_PROG > ${LOG_PATH}/${MONITOR_PROG}_startup.log 2>&1 &
    nohup ${MONITOR_PROG_PATH}/$MONITOR_PROG > /dev/null 2>&1 &
    renice  -5 $! 1 > /dev/null
    #renice  -20 $! 1 > /dev/null
    echo "[OK]"
}

stop() 
{
    if [ `id -u` -ne 0 ]; then
        echo "****** Critical ERROR: Please re-run as root or sudo ******"
        exit 1
    fi
    echo $"Stopping $MONITOR_PROG: "
    logger -p user.warning -t "${MONITOR_PROG}" "${MONITOR_PROG} is stopping" $(date +"%T %D")

    #stop vnet_xgw_monitor daemon
    if pgrep -x $MONITOR_PROG >/dev/null 2>&1;then
        pkill -9 -x $MONITOR_PROG
    fi

    #stop vnet_xgws
    if pgrep -x $PROG >/dev/null 2>&1;then
        pkill -9 -x $PROG
    fi
	
	echo "[OK]"
    return 0
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  status)
    status
    ;;
  *)
    echo $"Usage: service $MONITOR_PROG {start|stop|restart|status}"
    exit 1
esac
